<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="">
        <el-input style="width: 140px" v-model="queryParams.unitName" placeholder="单位名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button type="primary" @click="congzhiQuery">重置</el-button>
        <el-button @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="unitList" height="62vh">
      <el-table-column label="序号" type="index" />
      <el-table-column label="单位编号" prop="unitCode" />
      <el-table-column label="单位名称" prop="unitName" />
      <el-table-column label="单位等级" prop="unitType" />
      <el-table-column label="专业类型" prop="specialtyType" />
      <el-table-column label="所属单位编号" prop="parentCode" />
      <el-table-column label="单位简称" prop="unitAbbreviation" />
      <el-table-column label="状态" prop="hasDelete">
        <template #default="scope">
          <el-switch
            v-model="scope.row.hasDelete"
            class="ml-2"
            inline-prompt
            active-text="启用"
            inactive-text="禁用"
            :active-value="0"
            :inactive-value="1"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
            @change="changeUpdate(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" />
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改基础数据-单位数据对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="form" :model="forma" :inline="true" :rules="rules" label-width="120px">
        <el-form-item label="单位编号" prop="unitCode">
          <el-input style="width: 180px" v-model="forma.unitCode" placeholder="请输入单位编号" />
        </el-form-item>
        <el-form-item label="单位名称" prop="unitName">
          <el-input style="width: 180px" v-model="forma.unitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="单位等级" prop="unitType">
          <el-select filterable clearable style="width: 180px" v-model="forma.unitType" placeholder="请选择" @change="unitTypeChange(forma.unitType)">
            <el-option v-for="dict in unitTypeobj" :label="dict.propertyName" :value="dict.propertyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属单位编号" prop="parentCode" :required="forma.unitType !== '公司'">
          <el-select filterable clearable style="width: 180px" v-model="forma.parentCode" placeholder="请选择" :disabled="disabledpar">
            <el-option v-for="dict in parentCodeobj" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业类型" prop="specialtyType">
          <el-select filterable clearable style="width: 180px" v-model="forma.specialtyType" placeholder="请选择">
            <el-option v-for="dict in specialtyTypeobj" :label="dict.propertyName" :value="dict.propertyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位简称" prop="unitAbbreviation">
          <el-input style="width: 180px" v-model="forma.unitAbbreviation" placeholder="请输入单位简称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input style="width: 180px" v-model="forma.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="unit">
import { insertManBasicUnitapi, selectManBasicUnitListapi, editManBasicUnitapi, selectManBasicPropertyListapi } from "@/api/main/api.js";
import { reactive, ref, toRefs } from "vue";
defineExpose({ getList });
const emit = defineEmits(["parentCount"]);
const { proxy } = getCurrentInstance();
const loading = ref(true);
const title = ref("单位信息新增");
const open = ref(false);
const disabledpar = ref(false);
const data = reactive({
  editobj: {},
  // 基础数据-单位数据表格数据
  unitList: [],
  // 查询参数
  queryParams: {
    pageNum: "1",
    pageSize: "10",
    unitName: null,
  },
  //单位等级
  unitTypeobj: [
    // { value: "公司", label: "公司" },
    // { value: "分公司", label: "分公司" },
    // { value: "区段", label: "区段" },
    // { value: "工作区", label: "工作区" },
  ],
  //所属单位编号
  parentCodeobj: [],
  //专业类型
  specialtyTypeobj: [
    // { value: "工务专业", label: "工务专业" },
    // { value: "信号专业", label: "信号专业" },
    // { value: "通信专业", label: "通信专业" },
    // { value: "供电专业", label: "供电专业" },
  ],
  // 表单参数
  forma: {},
  // 表单校验
  rules: {
    unitCode: [{ required: true, message: "单位编号不能为空", trigger: ["blur", "change"] }],
    unitName: [{ required: true, message: "单位名称不能为空", trigger: ["blur", "change"] }],
    parentCode: [{ required: false, message: "所属单位编号不能为空", trigger: ["blur", "change"] }],
    specialtyType: [{ required: true, message: "专业类型不能为空", trigger: ["blur", "change"] }],
    unitType: [{ required: true, message: "单位等级不能为空", trigger: ["blur", "change"] }],
    sort: [{ required: true, message: "排序不能为空", trigger: ["blur", "change"] }],
  },
  propertyList: [],
});
const { propertyList, queryParams, forma, rules, unitList, unitTypeobj, parentCodeobj, specialtyTypeobj, editobj } = toRefs(data);

/** 查询基础数据-单位数据列表 */
const pageNumparent = ref(undefined);
const pageSizeparent = ref(undefined);
/** 查询列表 */
function getList(pageNum, pageSize) {
  loading.value = true;
  if (pageNum != undefined || pageSize != undefined) {
    pageNumparent.value = pageNum;
    pageSizeparent.value = pageSize;
    queryParams.value.pageNum = pageNum == undefined ? 1 : pageNum;
    queryParams.value.pageSize = pageSize == undefined ? 10 : pageSize;
  } else {
    queryParams.value.pageNum = pageNumparent.value == undefined ? "1" : pageNumparent.value;
    queryParams.value.pageSize = pageSizeparent.value == undefined ? "10" : pageSizeparent.value;
  }
  selectManBasicUnitListapi(queryParams.value).then((res) => {
    if (res.code == 200) {
      unitList.value = res.data;
      loading.value = false;
      emit("parentCount", res.count);
    }
  });
}
getList();
/** 查询基础数据-属性维护数据列表 */
function getList4() {
  selectManBasicPropertyListapi({ hasDelete: 0 }).then((res) => {
    if (res.code == 200) {
      propertyList.value = res.data;
      unitTypeobj.value = [];
      specialtyTypeobj.value = [];
      for (let i = 0; i < propertyList.value.length; i++) {
        if (propertyList.value[i].propertyType == "单位等级") {
          unitTypeobj.value.push(propertyList.value[i]);
        }
        if (propertyList.value[i].propertyType == "专业类型") {
          specialtyTypeobj.value.push(propertyList.value[i]);
        }
      }
    }
  });
}
getList4();
//单位等级
function unitTypeChange(row, isShow) {
  if (!isShow) {
    forma.value.parentCode = "";
    forma.value.parentId = "";
  }
  disabledpar.value = false;
  parentCodeobj.value = [];
  for (let i = 0; i < unitTypeobj.value.length; i++) {
    if (row == unitTypeobj.value[i].propertyName) {
      if (i != 0) {
        selectManBasicUnitListapi({ unitTypeId: unitTypeobj.value[i - 1].id, hasDelete: 0, id: forma.value.id }).then((res) => {
          if (res.code == 200) {
            for (let a = 0; a < res.data.length; a++) {
              const parentIdarr = {};
              parentIdarr.value = res.data[a].unitCode;
              parentIdarr.label = res.data[a].unitCode;
              parentIdarr.id = res.data[a].id;
              parentCodeobj.value.push(parentIdarr);
            }
          }
        });
      }
    }
    if (row == "公司") {
      disabledpar.value = true;
    }
  }
}
// 取消按钮
function cancel() {
  open.value = false;
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
function congzhiQuery() {
  // 查询参数
  queryParams.value = {
    unitName: null,
  };
  getList();
}
/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  forma.value = {
    unitCode: "",
    unitName: "",
    parentId: "",
    parentCode: "",
    unitType: "",
    unitAbbreviation: "",
    sort: "",
    unitTypeId: "",
    specialtyTypeId: "",
  };
  title.value = "单位信息新增";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  title.value = "单位信息修改";
  editobj.value = JSON.parse(JSON.stringify(row));
  forma.value = JSON.parse(JSON.stringify(row));
  open.value = true;
  unitTypeChange(row.unitType, true);
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["form"].validate((valid) => {
    if (valid) {
      for (let i = 0; i < unitTypeobj.value.length; i++) {
        if (forma.value.unitType == unitTypeobj.value[i].propertyName) {
          forma.value.unitTypeId = unitTypeobj.value[i].id;
        }
      }
      for (let i = 0; i < specialtyTypeobj.value.length; i++) {
        if (forma.value.specialtyType == specialtyTypeobj.value[i].propertyName) {
          forma.value.specialtyTypeId = specialtyTypeobj.value[i].id;
        }
      }
      for (let a = 0; a < parentCodeobj.value.length; a++) {
        if (parentCodeobj.value[a].value == forma.value.parentCode) {
          forma.value.parentId = parentCodeobj.value[a].id;
        }
      }
      if (forma.value.id != null) {
        const newforma = JSON.parse(JSON.stringify(forma.value));
        for (const key in editobj.value) {
          for (const keya in newforma) {
            if (key == keya && editobj.value[key] == newforma[keya] && key != "id") {
              Reflect.deleteProperty(newforma, keya);
              Reflect.deleteProperty(newforma, "params");
            }
          }
        }
        if (Object.keys(newforma).length <= 1) {
          open.value = false;
        } else {
          editManBasicUnitapi(newforma).then((res) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        }
      } else {
        insertManBasicUnitapi(forma.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
function changeUpdate(row) {
  forma.value = JSON.parse(JSON.stringify(row));
  editManBasicUnitapi({ hasDelete: forma.value.hasDelete, id: row.id }).then((res) => {
    proxy.$modal.msgSuccess("修改成功");
    open.value = false;
    getList();
  });
}
</script>
